package com.power.controller;

import com.power.constant.BusinessEnum;
import com.power.model.Result;
import com.power.domain.SysMenu;
import com.power.model.MenuAndAuth;
import com.power.service.SysMenuService;
import com.power.util.AuthUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;
    @Operation(summary ="获取用户权限表单和操作权限")
    @GetMapping("/nav")
    public Result getUserId()
    {
        //获取登录用户的id（已经在之前的Security容器里面了，所以使用AuthUtil工具进行获取）
        Long loginUserId = AuthUtils.getLoginUserId();
        //根据id查询用户权限表单集合（已经在之前的Security容器里面了，所以还是使用AuthUtil工具进行获取）
        Set<String> loginUserPerms = AuthUtils.getLoginUserPerms();
        //根据id查询用户操作权限集合
       Set<SysMenu> menuList= sysMenuService.getMenuListById(loginUserId);
        MenuAndAuth menuAndAuth=new MenuAndAuth(loginUserPerms,menuList);
        return Result.success(menuAndAuth);
    }
    @Operation(summary ="获取用户权限表单和操作权限")
    @GetMapping("/table")
    public Result getUserIdTable()
    {
//        //获取登录用户的id（已经在之前的Security容器里面了，所以使用AuthUtil工具进行获取）
//        Long loginUserId = AuthUtils.getLoginUserId();
//        //根据id查询用户权限表单集合（已经在之前的Security容器里面了，所以还是使用AuthUtil工具进行获取）
//        Set<String> loginUserPerms = AuthUtils.getLoginUserPerms();
//        //根据id查询用户操作权限集合
//        Set<SysMenu> menuList= sysMenuService.getMenuListById(loginUserId);
//        MenuAndAuth menuAndAuth=new MenuAndAuth(loginUserPerms,menuList);
//        return Result.success(menuAndAuth);
       List<SysMenu> list= sysMenuService.queryAllSysMenuList();
        return Result.success(list);
    }
    @PostMapping
    public Result addMenu(@RequestBody SysMenu sysMenu)
    {
        boolean save = sysMenuService.saveOne(sysMenu);
        return Result.handle(save);
    }
    @GetMapping("/info/{id}")
    public Result getMenuInfo(@PathVariable Long id)
    {
        SysMenu menu = sysMenuService.getById(id);
        return Result.success(menu);
    }
    //TODO 修改菜单
    @PutMapping
    public  Result UpdateMenu(@RequestBody SysMenu sysMenu)
    {
        boolean c=sysMenuService.updateMenuOne(sysMenu);
        return  c? Result.success("更新成功"):Result.fail(BusinessEnum.SERVER_INNER_ERROR);
    }

    //TODO 删除菜单
    @DeleteMapping("/{id}")
    public  Result DeleteById(@PathVariable("id") Long id)
    {
        boolean b=sysMenuService.deleteById(id);
        return  b? Result.success(null):Result.fail(BusinessEnum.SERVER_INNER_ERROR);
    }

}
